package com.jaya.budan.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.luozm.captcha.Captcha;

import org.json.JSONException;
import org.json.JSONObject;

public class Register_Activity extends BaseActivity implements View.OnClickListener {

    private Captcha captCha;
    private EditText et_register_mobile, et_register_password, et_register_rpassword;
    private Button btn_register_next;
    private RelativeLayout back, rl_register_mobile;
    private TextView tv_register_agree;
    private CheckBox cb_register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        captCha = findViewById(R.id.captCha);
        back = findViewById(R.id.back);
        rl_register_mobile = findViewById(R.id.rl_register_mobile);
        et_register_mobile = findViewById(R.id.et_register_mobile);
        et_register_password = findViewById(R.id.et_register_password);
        et_register_rpassword = findViewById(R.id.et_register_rpassword);
        tv_register_agree = findViewById(R.id.tv_register_agree);
        cb_register = findViewById(R.id.cb_register);
        btn_register_next = findViewById(R.id.btn_register_next);
        btn_register_next.setOnClickListener(this);
        back.setOnClickListener(this);
        rl_register_mobile.setOnClickListener(this);
        cb_register.setOnClickListener(this);
//        captCha.setBitmap(R.mipmap.register_yz);//设置背景图
//        captCha.setSeekBarStyle(R.mipmap.register_huadong, R.mipmap.register_huadong);//设置滑动框样式
//        if (captCha.getMode() == Captcha.MODE_BAR) {
//            captCha.setMode(Captcha.MODE_NONBAR);
//        } else {
//            captCha.setMode(Captcha.MODE_BAR);
//        }
        captCha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                ToolUtils.showToast(Register_Activity.this, "验证成功");
                btn_register_next.setEnabled(true);
                captCha.reset(false);
                return "验证通过";
            }
            @Override
            public String onFailed(int count) {
                ToolUtils.showToast(Register_Activity.this, "验证失败,失败次数" + count);
                btn_register_next.setEnabled(false);
                captCha.reset(true);
                return "验证失败";
            }
            @Override
            public String onMaxFailed() {
                ToolUtils.showToast(Register_Activity.this, "验证超过次数，你的帐号被封锁");
                captCha.reset(true);
                return "可以走了";
            }
        });
        SpannableString spanString = new SpannableString("《用户协议》");
        spanString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                //点击的响应事件
                ToolUtils.showToast(Register_Activity.this, "用户协议");
            }
        }, 0, spanString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanString.setSpan(new ForegroundColorSpan(Color.parseColor("#007AFF")), 0, spanString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableString spanString2 = new SpannableString("《隐私条款》");
        spanString2.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                //点击的响应事件
                ToolUtils.showToast(Register_Activity.this, "隐私条款");
            }
        }, 0, spanString2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanString2.setSpan(new ForegroundColorSpan(Color.parseColor("#007AFF")), 0, spanString2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_register_agree.setText("注册代表我同意不单");
        tv_register_agree.append(spanString);
        tv_register_agree.append("和");
        tv_register_agree.append(spanString2);
        tv_register_agree.append(", 且承诺，我已满十八周岁，并清楚了解不单平台禁止十八周岁以下未成年注册及使用的各项规定。");
        tv_register_agree.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_register_next:
                Intent intent = new Intent(Register_Activity.this, Register2_Activity.class);
                intent.putExtra("username",et_register_mobile.getText().toString().trim());
                intent.putExtra("password",et_register_password.getText().toString().trim());
                intent.putExtra("rpassword",et_register_rpassword.getText().toString().trim());
                startActivity(intent);
                break;
            case R.id.cb_register:
                if (cb_register.isChecked()){
                    cb_register.setChecked(true);
                    btn_register_next.setEnabled(true);
                } else {
                    cb_register.setChecked(false);
                    btn_register_next.setEnabled(false);
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.rl_register_mobile:
                ToolUtils.showSoftInputFromWindow(et_register_mobile);
                break;
            default:
                break;
        }
    }
}
